package com.opennews.openplatform

import grails.compiler.GrailsCompileStatic
import grails.converters.JSON
import grails.gorm.transactions.Transactional

@Transactional(readOnly = true)
@GrailsCompileStatic
class AppErrorReportService {
    /**
     * Saves error report sent from app.
     * @param data: map instance which contains keys below.
     *          errorMessage: String of errorMessage.
     *          stackTrace: String of stackTrace.
     *          systemInfo: Map of String of systemInfo.
     */
    @Transactional
    void save(Map<String, ?> data) {
        // If deviceInfo contains map instance then we need to converts it to string type.
        if (data.deviceInfo instanceof Map) {
            data.deviceInfo = (data.deviceInfo as JSON).toString()
        }

        // If systemInfo contains map instance then we need to converts it to string type.
        if (data.systemInfo instanceof Map) {
            data.systemInfo = (data.systemInfo as JSON).toString()
        }

        // Creates a new AppErrorReport with provided data.
        new AppErrorReport(data).save()
    }
}
